package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStorageItemListAdapter;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;


//验收入库
public class RefundStorageOverviewActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView refundStorageItemList;

    private List<RefundStorageOverviewBean.DataBean.MaterielListBean> allData;

    private RefundStorageItemListAdapter adapter;

    private String mRefundStoragePickingNumber, mMaterielCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_overview);
        initView(RefundStorageOverviewActivity.this);
        SPUtils.removeRefundStorageInformation(RefundStorageOverviewActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(RefundStorageOverviewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refundStorageItemList = (PullableListView) activity.findViewById(R.id.refundStorageItemList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.refundStorageItemListRefresh);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
    }

    private void initData(Activity activity) {
        title.setText("退库概览");
        ActivityUtils.initBackAndFunction(activity, "列表", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(RefundStorageOverviewActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();
            }
        });

        refundStorageItemList.setPullUpFlag(false);
        refundStorageItemList.setPullDownFlag(false);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check(activity)) {
                    Intent intent = new Intent(activity, RefundStorageAddOperatorActivity.class);
                    startActivity(intent);
                }
            }
        });
        getExtra(activity);
        mMaterielCode = "";
        getData(activity);
    }

    private boolean check(Context context) {
        int confirmNumber = RefundStorageUtils.getRefundStorageNumber(context);
        if (confirmNumber == 0) {
            Utils.middleToast(context, "请至少完善一个行项目");
            return false;
        }
        return true;
    }

    private void getExtra(Activity activity) {
        mRefundStoragePickingNumber = activity.getIntent().getStringExtra(RefundStorageExtra.REFUND_STORAGE_PICKING_NUMBER);
    }

    private void addRefundStorageItemListData(Context context, RefundStorageOverviewBean.DataBean bean, List<RefundStorageOverviewBean.DataBean.MaterielListBean> refundStorageItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new RefundStorageItemListAdapter(context, bean, refundStorageItemList, mMaterielCode, (String materielCode) -> {
                mMaterielCode = materielCode;
                getData(context);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context) {
        String refundStorageInformation = SPUtils.getRefundStorageInformation(context);
        if ("".equals(refundStorageInformation)) {
            RefundStorageOverviewRequest.request(context, mRefundStoragePickingNumber, new GsonBeanListener<RefundStorageOverviewBean>() {
                @Override
                public void onSuccess(RefundStorageOverviewBean bean) {
                    RefundStorageUtils.saveRefundStorageOverviewBean(context, bean);
                    RefundStorageOverviewBean.DataBean data = bean.getData();
                    allData = bean.getData().getMaterielList();
                    addRefundStorageItemListData(context, data, ActivityUtils.getShowData(allData, 0), refundStorageItemList, true);
                }
            });
        } else {
            Gson gson = new Gson();
            RefundStorageOverviewBean bean = gson.fromJson(refundStorageInformation, RefundStorageOverviewBean.class);
            if ("".equals(mMaterielCode) || mMaterielCode == null) {
                allData = bean.getData().getMaterielList();
            } else {
                allData = RefundStorageUtils.getRefundStorageMaterielList(context, mMaterielCode);
            }
            addRefundStorageItemListData(context, bean.getData(), ActivityUtils.getShowData(allData, 0), refundStorageItemList, true);
        }

    }

    public interface RefundStorageOverviewListener {
        void onNoDoubleClickListener(String materielCode);
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(RefundStorageOverviewActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                finish();
            }
        }).show();
    }
}
